package com.young.jdbc;

import java.io.FileInputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @Author: young
 * @Date: 2021-12-03 0:02
 * @Description:演示select语句返回ResultSet（结果集）并取出结果
 */
public class ResultSet_demo {
    public static void main(String[] args) throws Exception {
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properties"));
        String url = properties.getProperty("url");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");

        Class.forName(driver);

        Connection connection = DriverManager.getConnection(url, user, password);

        String sql = "select * from student";

        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);

        while(resultSet.next()){
            int id = resultSet.getInt(1);
            String name = resultSet.getString(2);
            String sex = resultSet.getString(3);
            Date date = resultSet.getDate(4);
            String phone = resultSet.getString(5);
            System.out.println(id+"\t"+name+"\t"+sex+"\t"+date+"\t"+phone);
        }
        resultSet.close();
        statement.close();
        connection.close();

    }
}
